package com.study.array;

/**
 * @author ytj
 * @date 2021/12/20
 */
public class Method5 {
    public static void main(String[] args) {
        int[] nums=new int[]{1,3,5,6};
        int target=0;
        int result=Method5.searchInsert(nums,target);
        System.out.println(result);
    }

    public static int searchInsert(int[] nums, int target) {
        if(target>nums[nums.length-1]){
            return nums.length;
        }
        if(target<nums[0]){
            return 0;
        }
        int left=0;
        int right=nums.length-1;
        int mind=0;
        while (left<=right){
            mind=(left+right)/2;
            if(nums[mind]==target){
                return mind;
            }else if(target>nums[mind]){
                left=left+1;
            }else if(target<nums[mind]&&target>nums[mind-1]){
                return mind;
            }else if(target<nums[mind]){
                right=right-1;
            }
        }
        return 0;
    }
}
